<?php

namespace api\controller;

use common\controller\VerifyController;
use common\exception\LogicException;
use common\service\TenantGroupMemberService;
use think\Request;
use think\Exception;

class TenantGroupMemberController extends VerifyController
{
    protected $tenantGroupMember;

    public function __construct()
    {
        parent::__construct();
        $this->tenantGroupMember = new TenantGroupMemberService();
    }

    /**
     * 查找公司列表
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function select(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupMember->select($param);
            return json(success_response('', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 新增集团成员
     * @param Request $request
     * @return \think\Response
     * @throws \Throwable
     */
    public function add(Request $request)
    {
        $param = $request->param();
        try {
            $resData = $this->tenantGroupMember->add($param['data']);
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 退出集团
     * @param Request $request
     * @return \think\Response
     */
    public function quit(Request $request)
    {
        try {
            $param = $request->param();
            $resData = $this->tenantGroupMember->quit($param, $request->getTenantId());
            return json(success_response('Success', $resData));
        } catch (LogicException $e) {
            throw_strack_exception($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 获取集团下的供应商
     * @param Request $request
     * @return \think\Response
     */
    public function selectTenantGroupMemberSupplier(Request $request)
    {
        $param = $request->param()["param"];
        $param["filter"]["tenant_id"] = $param["filter"]["tenant_id"] ?? $request->getTenantId();
        $resData = $this->tenantGroupMember->selectTenantGroupMemberSupplier($param);
        return json(success_response('Success', $resData));
    }

    /**
     * 获取当前公司的集团
     * @param Request $request
     * @return \think\Response
     */
    public function getCurrentTenantGroup(Request $request)
    {
        $param = $request->param()["param"];
        $param["filter"]["tenant_group_member.tenant_id"] = $param["filter"]["tenant_group_member.tenant_id"] ?? $request->getTenantId();
        $resData = $this->tenantGroupMember->getTenantGroup($param);
        return json(success_response('Success', $resData));
    }

    /**
     * 根据项目获取集团成员
     * @param Request $request
     * @return \think\Response
     */
    public function getTenantGroupMemberByProject(Request $request)
    {
        $param = $request->param()["param"];
        $resData = $this->tenantGroupMember->getTenantGroupMemberByProject($param);
        return json(success_response('Success', $resData));
    }

    /**
     * list
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function list(Request $request)
    {
        $param = $request->param();
        $resData = $this->tenantGroupMember->list($param['param']);
        return json(success_response('', $resData));
    }
}